/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-04
 * Time: 14:33
 */

public class LinkedList01 {
    public static void main(String[] args) {
        //模拟简单的双向链表

        //创建三个节点
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node hsp = new Node("老汉");

        //连接三个节点 形成双向链表
        jack.next = tom;
        tom.next = hsp;
        //
        hsp.pro = tom;
        tom.pro = jack;
        //双向链表的形成

        Node first = jack;  //头节点
        Node last = hsp;  //尾节点

        // 从头到尾的遍历
        while (true){
            if (first == null){
                break;
            }
            System.out.println(first);
            first = first.next;
        }

        //从尾到头的遍历
        while (true){
            if (last == null){
                break;
            }
            //输出last
            System.out.println(last.item);
            last = last.pro;
        }

        //添加一个节点
        //在tom后面插入 smith节点

        //1.创建一个Node节点
        Node smith = new Node("smith");
        smith.next = hsp;
        smith.pro = tom;
        hsp.pro = smith;
        tom.next = smith;

        System.out.println("----------------");
        first = jack;
        while (true){
            if (first == null){
                break;
            }
            System.out.println(first);
            first = first.next;
        }
    }
}

//定义一个Node类 表示双向链表的节点
class Node{
    public Object item; //存放数据
    public Node next; //存放下一个节点
    public Node pro; //指向前一个节点

    public Node(Object name){
        this.item = name;
    }

    public String toString(){
        return "Node name=" + item;
    }
}
